#include "StdAfx.h"
#include "YGoodsTypeTableCmd.h"
#include "..\DataLayer\YGoodsTypeTab.h"

YGoodsTypeTableCmd::YGoodsTypeTableCmd(void)
{
}

YGoodsTypeTableCmd::~YGoodsTypeTableCmd(void)
{
}

BOOL YGoodsTypeTableCmd::Add(YGoodsTypeTab* pYGoodsTypeTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->AddNew();
		if ( bRet )
		{
			CString strCode(pYGoodsTypeTab->GetCode());
			CString strName(pYGoodsTypeTab->GetName());
			CString strDescription(pYGoodsTypeTab->GetDesc());
			strCode = SqlFilterToDb(strCode);
			strName = SqlFilterToDb(strName);
			strDescription = SqlFilterToDb(strDescription);

			pRecordset->SetFieldValue( _T("ygt_code"),strCode);
			pRecordset->SetFieldValue( _T("ygt_name"),strName);
			pRecordset->SetFieldValue( _T("ygt_description"),strDescription);
			pRecordset->SetFieldValue( _T("ygt_parentid"),pYGoodsTypeTab->GetParentId());
			bRet = pRecordset->Update();
			if ( bRet )
			{
				int nId = -1;
				pRecordset->GetFieldValue( _T("ygt_id"),nId );
				pYGoodsTypeTab->SetId(nId);
			}
		}
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YGoodsTypeTableCmd::DelByID(int nID)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type where ygt_id=%d"),nID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->Delete();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YGoodsTypeTableCmd::EditByID(YGoodsTypeTab* pYGoodsTypeTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type where ygt_id=%d"),pYGoodsTypeTab->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		pRecordset->Edit();
		CString strCode(pYGoodsTypeTab->GetCode());
		CString strName(pYGoodsTypeTab->GetName());
		CString strDescription(pYGoodsTypeTab->GetDesc());
		strCode = SqlFilterToDb(strCode);
		strName = SqlFilterToDb(strName);
		strDescription = SqlFilterToDb(strDescription);

		pRecordset->SetFieldValue( _T("ygt_code"),strCode);
		pRecordset->SetFieldValue( _T("ygt_name"),strName);
		pRecordset->SetFieldValue( _T("ygt_description"),strDescription);
		pRecordset->SetFieldValue( _T("ygt_parentid"),pYGoodsTypeTab->GetParentId());
		bRet = pRecordset->Update();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

void YGoodsTypeTableCmd::GetByID(YGoodsTypeTab* pYGoodsTypeTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type where ygt_id=%d"),pYGoodsTypeTab->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strDescription = _T("");
		int nParentid = -1;
		int nFloor = -1;
		pRecordset->GetFieldValue( _T("ygt_id"),nId);
		pRecordset->GetFieldValue( _T("ygt_code"),strCode);
		pRecordset->GetFieldValue( _T("ygt_name"),strName);
		pRecordset->GetFieldValue( _T("ygt_description"),strDescription);
		pRecordset->GetFieldValue( _T("ygt_parentid"),nParentid);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strDescription = SqlFilterToStr(strDescription);

		pYGoodsTypeTab->SetId(nId);
		pYGoodsTypeTab->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYGoodsTypeTab->SetName( (LPTSTR)(LPCTSTR)strName );
		pYGoodsTypeTab->SetDesc( (LPTSTR)(LPCTSTR)strDescription );
		pYGoodsTypeTab->SetParentId( nParentid );
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

void YGoodsTypeTableCmd::GetByParentID(int nParentID,CTypedPtrList<CPtrList, YGoodsTypeTab *>*pRecList)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type where ygt_parentid=%d"),nParentID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	while ( !pRecordset->IsEof() )
	{
		YGoodsTypeTab* pYGoodsTypeTab = new YGoodsTypeTab;
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strDescription = _T("");
		int nParentid = -1;
		int nFloor = -1;
		pRecordset->GetFieldValue( _T("ygt_id"),nId);
		pRecordset->GetFieldValue( _T("ygt_code"),strCode);
		pRecordset->GetFieldValue( _T("ygt_name"),strName);
		pRecordset->GetFieldValue( _T("ygt_description"),strDescription);
		pRecordset->GetFieldValue( _T("ygt_parentid"),nParentid);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strDescription = SqlFilterToStr(strDescription);

		pYGoodsTypeTab->SetId(nId);
		pYGoodsTypeTab->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYGoodsTypeTab->SetName( (LPTSTR)(LPCTSTR)strName );
		pYGoodsTypeTab->SetDesc( (LPTSTR)(LPCTSTR)strDescription );
		pYGoodsTypeTab->SetParentId( nParentid );
		pRecList->AddTail(pYGoodsTypeTab);

		pRecordset->MoveNext();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

void YGoodsTypeTableCmd::Get(CTypedPtrList<CPtrList, YGoodsTypeTab *>*pRecList)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods_type"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	while ( !pRecordset->IsEof() )
	{
		YGoodsTypeTab* pYGoodsTypeTab = new YGoodsTypeTab;
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strDescription = _T("");
		int nParentid = -1;
		int nFloor = -1;
		pRecordset->GetFieldValue( _T("ygt_id"),nId);
		pRecordset->GetFieldValue( _T("ygt_code"),strCode);
		pRecordset->GetFieldValue( _T("ygt_name"),strName);
		pRecordset->GetFieldValue( _T("ygt_description"),strDescription);
		pRecordset->GetFieldValue( _T("ygt_parentid"),nParentid);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strDescription = SqlFilterToStr(strDescription);

		pYGoodsTypeTab->SetId(nId);
		pYGoodsTypeTab->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYGoodsTypeTab->SetName( (LPTSTR)(LPCTSTR)strName );
		pYGoodsTypeTab->SetDesc( (LPTSTR)(LPCTSTR)strDescription );
		pYGoodsTypeTab->SetParentId( nParentid );
		pRecList->AddTail(pYGoodsTypeTab);

		pRecordset->MoveNext();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

int YGoodsTypeTableCmd::SameNameRecordCount(TCHAR* pName,int nID,int nParentID)
{
	CString strSql = _T("");
	CString strName = SqlFilterToDb(pName);
	strSql.Format(_T("select * from ys_goods_type where ygt_name=\'%s\' and ygt_id <> %d and ygt_parentid=%d"),strName,nID,nParentID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	bRet = pRecordset->GetRecordCount();
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return (int)bRet;
}

int YGoodsTypeTableCmd::SameNameRecordCount(TCHAR* pName, int nParentID)
{
	CString strSql = _T("");
	CString strName = SqlFilterToDb(pName);
	strSql.Format(_T("select * from ys_goods_type where ygt_name=\'%s\' and ygt_parentid=%d"),strName,nParentID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	bRet = pRecordset->GetRecordCount();
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return (int)bRet;
}